package com.smart.business.controller;

import com.smart.aop.log.LogType;
import com.smart.aop.log.SaveLog;
import com.smart.aop.permission.HasPermission;
import com.smart.common.utils.DateUtil;
import com.smart.entity.business.JwznWatDataEntity;
import com.smart.model.excel.annotation.ExcelField;
import com.smart.model.response.r.Result;
import com.smart.model.response.r.ResultCode;
import com.smart.model.response.r.ResultWrapper;
import com.smart.service.business.JwznWatDataService;
import com.smart.tools.excel.ExcelExport;
import com.smart.tools.excel.ExcelImport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;

/**
 * wat Controller
 *
 * @author wf
 * @since 2025-05-13 17:29:18
 */
@RestController
@RequestMapping("/business/jwznWatData")
public class JwznWatDataController {
    @Autowired
    JwznWatDataService jwznWatDataService;
    @Autowired
    ResultWrapper resultWrapper;

    /**
     * 列表
     *
     * @param jwznWatDataEntity watbean
     * @return String
     */
    @GetMapping("/page")
    public String page(JwznWatDataEntity jwznWatDataEntity) {
        return Result.data(jwznWatDataService.findPage(jwznWatDataEntity));
    }

    /**
     * 列表（不分页）
     *
     * @param jwznWatDataEntity watbean
     * @return String
     */
    @GetMapping("/list")
    public String list(JwznWatDataEntity jwznWatDataEntity) {
        return Result.data(jwznWatDataService.findList(jwznWatDataEntity));
    }

    /**
     * 信息
     *
     * @param id 主键ID
     * @return String
     */
    @GetMapping("/get")
    public String get(@RequestParam String id) {
        JwznWatDataEntity result = jwznWatDataService.get(id);
        return result != null ? Result.data(result) : Result.fail(ResultCode.FAIL);
    }

    /**
     * 保存
     *
     * @param jwznWatDataEntity watbean
     * @return String
     */
    @HasPermission("jwznWatData:add")
    @PostMapping("/save")
    @SaveLog(module = "wat管理" , type = LogType.ADD)
    public String save(@RequestBody JwznWatDataEntity jwznWatDataEntity) {
        JwznWatDataEntity result = jwznWatDataService.saveEntity(jwznWatDataEntity);
        return result != null ? Result.data(result) : Result.fail(ResultCode.FAIL);
    }

    /**
     * 修改
     *
     * @param jwznWatDataEntity watbean
     * @return String
     */
    @HasPermission("jwznWatData:update")
    @PostMapping("/update")
    @SaveLog(module = "wat管理" , type = LogType.UPDATE)
    public String update(@RequestBody JwznWatDataEntity jwznWatDataEntity) {
        JwznWatDataEntity result = jwznWatDataService.updateEntity(jwznWatDataEntity);
        return result != null ? Result.data(result) : Result.fail(ResultCode.FAIL);
    }

    /**
     * 删除
     *
     * @param jwznWatDataEntity watbean
     * @return String
     */
    @HasPermission("jwznWatData:delete")
    @PostMapping("/delete")
    @SaveLog(module = "wat管理" , type = LogType.DELETE)
    public String delete(@RequestBody JwznWatDataEntity jwznWatDataEntity) {
        return Result.status(jwznWatDataService.delete(jwznWatDataEntity));
    }

    /**
     * 导出
     *
     * @param jwznWatDataEntity watbean
     * @param response 响应
     */
    @HasPermission("jwznWatData:export")
    @PostMapping(value = "/export")
    public void exportData(@RequestBody JwznWatDataEntity jwznWatDataEntity, HttpServletResponse response) {
        List<JwznWatDataEntity> list = resultWrapper.setListIdentityInfo(jwznWatDataService.findList(jwznWatDataEntity), JwznWatDataEntity.class);
        String fileName = "wat数据" + DateUtil.getDate("yyyyMMddHHmmss") + ".xlsx";
        try (ExcelExport export = new ExcelExport("wat数据", JwznWatDataEntity.class)) {
            export.setDataList(list).write(response, fileName);
        }
    }

    /**
     * 导入
     *
     * @param request 请求
     */
    @HasPermission("jwznWatData:import")
    @PostMapping(value = "/import")
    public String importData(MultipartHttpServletRequest request) {
        MultiValueMap<String, MultipartFile> multiFileMap = request.getMultiFileMap();
        List<MultipartFile> files = multiFileMap.get("files");
        try (ExcelImport excelImport = new ExcelImport(files.get(0), 2, 0)) {
            List<JwznWatDataEntity> dataList = excelImport.getDataList(JwznWatDataEntity.class);
            return Result.status(jwznWatDataService.saveBatch(dataList));
        } catch (Exception e) {
            return Result.fail("导入失败！");
        }
    }

    /**
     * 下载导入模板
     *
     * @param response 响应
     */
    @GetMapping(value = "/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response) {
        String fileName = "导入wat模板.xlsx";
        try (ExcelExport export = new ExcelExport("wat数据", JwznWatDataEntity.class, null, ExcelField.Type.IMPORT)) {
            export.write(response, fileName);
        }
    }

    @GetMapping("/yesterLineChart")
    public String yesterLineChart(@RequestParam String name) {
        return Result.data(jwznWatDataService.getYesterLineChartData(name));
    }
    /**
     * 获取一天的折线图数据
     * @param name 水表名称
     * @return 折线图数据
     */

    @GetMapping("/dailyLineChart")
    public String dailyLineChart(@RequestParam String name) {
        return Result.data(jwznWatDataService.getDailyLineChartData(name));
    }

    /**
     * 获取一周的折线图数据
     * @param name 水表名称
     * @return 折线图数据
     */
    @GetMapping("/weeklyLineChart")
    public String weeklyLineChart(@RequestParam String name) {
        return Result.data(jwznWatDataService.getWeeklyLineChartData(name));
    }

    /**
     * 获取一月的折线图数据
     * @param name 水表名称
     * @return 折线图数据
     */
    @GetMapping("/monthlyLineChart")
    public String monthlyLineChart(@RequestParam String name) {
        return Result.data(jwznWatDataService.getMonthlyLineChartData(name));
    }

    /**
     * 获取饼图数据,下级查看
     * @param name
     * @return
     */
    @GetMapping("/yesterPieChart")
    public String getYesterPieChartDataByLevel(@RequestParam String name) {
        return Result.data(jwznWatDataService.getYesterPieChartDataByLevel(name));
    }

    @GetMapping("/dailyPieChart")
    public String getDailyPieChartDataByLevel(@RequestParam String name) {
        return Result.data(jwznWatDataService.getDailyPieChartDataByLevel(name));
    }

    @GetMapping("/weeklyPieChart")
    public String getWeeklyPieChartDataByLevel(@RequestParam String name) {
        return Result.data(jwznWatDataService.getWeeklyPieChartDataByLevel(name));
    }

    @GetMapping("/monthlyPieChart")
    public String getMonthlyPieChartDataByLevel(@RequestParam String name) {
        return Result.data(jwznWatDataService.getMonthlyPieChartDataByLevel(name));
    }

    /**
     * 获取柱状图数据,获取全部name和对应name的给定时间段内的总consume
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/barChart")
    public String barChart(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
                           @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime){
        //给定picktime(两个),查询出name和对应的consume
        return Result.data(jwznWatDataService.getBarChartDataBy2Time(startTime, endTime));
    }

    /**
     * 获取水表的消耗统计数据(当日、当周、当月、当年)
     * @param name 水表名称
     * @return 包含消耗统计数据的Map
     */
    @GetMapping("/dailyConsumptionStats")
    public String getDailyConsumptionStats(@RequestParam String name) {
        return Result.data(jwznWatDataService.getDailyConsumptionStats(name));
    }

    @GetMapping("/yesterConsumptionStats")
    public String getYesterConsumptionStats(@RequestParam String name) {
        return Result.data(jwznWatDataService.getYesterConsumptionStats(name));
    }

    @GetMapping("/weeklyConsumptionStats")
    public String getWeeklyConsumptionStats(@RequestParam String name) {
        return Result.data(jwznWatDataService.getWeeklyConsumptionStats(name));
    }

    @GetMapping("/monthlyConsumptionStats")
    public String getMonthlyConsumptionStats(@RequestParam String name) {
        return Result.data(jwznWatDataService.getMonthlyConsumptionStats(name));
    }
}
